package face.pay.open.validate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.face.pay.SecurityConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import face.pay.dao.OemConfigInfoDao;
import face.pay.open.util.request.ApiRequestParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 参数验证
 * 主要验证 数据加密
 */
@Component
@Slf4j
public class ApiParamValidate {

    @Autowired
    OemConfigInfoDao oemConfigInfoDao;

    public ApiRequestParam parse(String json){

        log.info("解析参数");
        try{
            ApiRequestParam  apiRequestParam = JSON.parseObject(json,ApiRequestParam.class);
            log.info("is ok "+apiRequestParam.isoK());
            if(apiRequestParam.isoK()){
                return  apiRequestParam;
            }

        }catch (Exception e){
            log.error("参数解解析失败",e);
        }
        return  null;
    }

    public boolean validate(Map<String,String> data, String sign, SecurityConfig securityConfig){

        try{

            Boolean validate =  WXPayUtil.isSignatureValid(data,securityConfig.getFaceKey(),sign);

            if(validate){

                return  true;

            }

        }catch (Exception e){
            log.error("验证签名异常",e);
        }

        return  false;
    }




}
